<template>
  <el-container>
    <el-header>
      <img src="../assets/images/logo.png" alt="" />
      <el-menu
        mode="horizontal"
        background-color="#23262e"
        text-color="#fff"
        active-text-color="rgb(64, 158, 255)"
        default-active="aaa"
        unique-opened
      >
        <el-submenu index="bbb">
          <template slot="title">
            <img class="avatar" :src="user.user_pic" alt="" v-if="user.user_pic" />
            <img class="avatar" src="@/assets/logo.png" alt="" v-else />
            个人中心
          </template>
          <el-menu-item index="b1"><i class="el-icon-edit"></i>用户资料</el-menu-item>
          <el-menu-item index="b2"><i class="el-icon-edit"></i>重置密码</el-menu-item>
          <el-menu-item index="b2"><i class="el-icon-edit"></i>更换头像</el-menu-item>
        </el-submenu>
        <el-menu-item index="5" @click="fn"><i class="el-icon-edit"></i>退出</el-menu-item>
      </el-menu>
    </el-header>
    <el-container>
      <el-aside width="200px">
        <el-menu
          mode="vertical"
          background-color="#23262e"
          text-color="#fff"
          active-text-color="rgb(64, 158, 255)"
          default-active="aaa"
          unique-opened
        >
          <div class="user-info">
            <img class="avatar" :src="user.user_pic" alt="" v-if="user.user_pic" />
            <img class="avatar" src="@/assets/logo.png" alt="" v-else />
            <p>
              欢迎你 <span> {{ user.nickname || user.username }} </span>
            </p>
          </div>
          <template v-for="item in menu">
            <el-menu-item
              :index="item.indexPath"
              :key="item.indexPath"
              v-if="item.children === null"
              ><i :class="item.icon"></i> {{ item.title }}</el-menu-item
            >

            <el-submenu :index="item.indexPath" :key="item.indexPath" v-else>
              <template slot="title"><i :class="item.icon"></i>{{ item.title }}</template>
              <el-menu-item index="2"><i class="el-icon-edit"></i>类别管理</el-menu-item>
              <el-menu-item index="2-1"><i class="el-icon-edit"></i>文章管理</el-menu-item>
            </el-submenu>
          </template>
        </el-menu>
      </el-aside>
      <el-main>123 </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { mapState } from 'vuex'
import { getMenuAPI } from '@/api/menu'
export default {
  name: 'MyHome',
  data() {
    return {
      menu: []
    }
  },
  created() {
    this.$store.dispatch('user/getUser')
    this.getMenu()
  },
  computed: {
    ...mapState('user', ['user'])
  },
  methods: {
    fn() {
      this.$confirm('确定退出吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 清除 token
          this.$store.commit('user/updataToken', '')
          this.$store.commit('user/updatedUser', {})
          // 跳转到 注册
          this.$router.push('/reg')
        })
        .catch(() => {})
    },
    async getMenu() {
      const { data: res } = await getMenuAPI()
      console.log(res)
      if (res.code === 0) {
        this.menu = res.data
      }
    }
  }
}
</script>

<style lang="less" scoped>
.el-header {
  background-color: #23262e;
  height: 60px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.el-aside {
  background-color: #23262e;
  color: #333;
  line-height: 200px;
  .el-menu {
    width: 200px;
    overflow: hidden;
  }
}

.el-main {
  background-color: #f2f2f2;
  color: #333;
  text-align: center;
  line-height: 160px;
}

.el-container {
  height: 100%;
}

.avatar {
  height: 35px;
  width: 35px;
  border-radius: 50%;
  // 下面的样式是，让图片按照原比例，完整的展示
  object-fit: cover;
  background-color: #fff;
  margin-right: 10px;
}
.user-info {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 70px;
  color: #fff;
}
.el-menu-item,
/deep/ .el-submenu__title {
  font-size: 13px;
}
</style>
